<?php	namespace Admin\Controller;
class GoodsController extends CommonController
{
	protected $db;
	public function __init()
	{
		$this -> db = new \Common\Model\Goods;
	}
	/*
	 * 商品列表页
	 */
	public function index()
	{
		//关联货品，商品，获取所有数据
		$GoodsData = $this->db->get();
//		p($GoodsData);
		//数据分配
		View::with('GoodsData',$GoodsData);
		View::make();
	}
	/*
	 * 添加
	 */
	public function add()
	{
		if(IS_POST)
		{
			if(!$this->db->store())
			{
				View::error($this->db->getError());
			}else
			{
				View::success('添加成功',U('index'));
			}
		}
		//获取分类所有数据
		$CateData = Db::table('cate')->get();
//		p($CateData);
		//获取品牌所有数据
		$BrandData = Db::table('brand')->get();
//		p($BrandData);
		//分配品牌
		View::with('BrandData',$BrandData);
		//将分类转为树状
		$CateData = Data::tree($CateData,'cname','cid','cfid');
		//分配数据
		View::with('CateData',$CateData);
		View::make();
	}
	/*
	 * ajax分类显示商品属性，用的一步再拼接
	 */
	public function addajax()
	{
//		p($_POST['lid']);
		if(IS_AJAX){
			//抓取类型ID
			$lid = $_POST['lid'];
			//获取类型属性所有数据，条件为类型ID
			$TypeattrData = Db::table('typeattr')->where('typeattr_lid',$lid)->get();
			//将字符转为数组
			foreach ($TypeattrData as $k => $v) {
				$TypeattrData[$k]['xvalue'] = explode(',', $v['xvalue']);
			}
			//ajax页面输出
			echo json_encode($TypeattrData);
		}
	}
	/*
	 * ajax的图片路径创建，获取数据，并传出
	 */
	public function ajaxUpload()
	{
		$files = Upload::path("Upload/goods/".date("ymd"))->make();
//		p($files);
		if($files){
			View::ajax($files[0]);
		}
		View::ajax('上传错误');
	}
	/*
	 * 图片的删除，异步
	 */
	public function delimg()
	{
		if(IS_AJAX)
		{
			//异步抓取图片路径
			$path = $_POST['path'];
//			p($path);
//			p(1);
			//删除路径
			if(unlink($path)){
				View::ajax(1);
			}
			View::ajax(0);
		}
	}
	//商品编辑
	public function edit()
	{
		//获取商品ID
		$sid = Q('get.sid',0,'intval');
		if(IS_POST)
		{
			if(!$this->db->edit($sid))
			{
				View::error($this->db->getError());
			}else
			{
				View::success('编辑成功',U('index'));
			}
		}
//		p($sid);
		//获取商品旧数据，条件为sid
		$OldData = $this->db->where('sid',$sid)->first();
//		p($OldData);
		//将旧数据分配特免
		View::with('OldData',$OldData);
		//获取类型ID
		$lid = $OldData['typeattr_lid'];
//		p($lid);
		//查看类型属性，条件为lid，关联商品属性
		$TypeattrData = Db::table('typeattr')->where('typeattr_lid',$lid)->join('goodsattr','xid','=','attribute_xid')->where('goods_sid',$sid)->get();
		foreach ($TypeattrData as $k => $v) {
				$TypeattrData[$k]['xvalue'] = explode(',', $v['xvalue']);
			}
//		p($TypeattrData);
		View::with('TypeattrData',$TypeattrData);
		$Oldgoodsattrdata = Db::table('goodsattr')->where('goods_sid',$sid)->get();
//		p($Oldgoodsattrdata);
		foreach ($Oldgoodsattrdata as $k => $v) {
			$shopvalue[] = $v['shopvalue'];
			$addition[] = $v['addition'];
			View::with('addition',$v['addition']);
			View::with('shopvalue',$v['shopvalue']);
		}
//		p($shopvalue);
		//获取商品详情数据
		$OlddetailsData = Db::table('details')->where('details_sid',$sid)->first();
			$OlddetailsData['small'] = explode(',', $OlddetailsData['small']);
			$OlddetailsData['mean'] = explode(',', $OlddetailsData['mean']);
			$OlddetailsData['big'] = explode(',', $OlddetailsData['big']);
		View::with('OlddetailsData',$OlddetailsData);
		//查看分类
		$CateData = Db::table('cate')->get();
//		p($CateData);
		//查看品牌
		$BrandData = Db::table('brand')->get();
//		p($BrandData);
		View::with('BrandData',$BrandData);
		$CateData = Data::tree($CateData,'cname','cid','cfid');
		View::with('CateData',$CateData);
		View::make();
	}
	public function del()
	{
		$sid = Q('get.sid',0,'intval');
		$this->db->del($sid);
		View::success('删除成功',U('index'));
	}
}
